import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    noreadmsglist:[],  //未读信息
    searchlist:[],
    chapter:[],
    //章节id数组
    chapteridarr:[],

    //当前阅读时的chapterid 的index值
    chapterid:0,

    //当前阅读时的articleid
    articleid:0,

    //获取的章节信息
    chapterdata:{},

     //当前读者信息
     readerdata:{},
  },
  mutations: {
    //存未读信息
    putnoreadmsglist(state,data){
      state.noreadmsglist=data
    },
    //当前读者信息
    putreaderdata(state,data){
      state.readerdata=data
    },

     //当前阅读时的articleid
     putarticleid(state,data){
      state.articleid=data
    },

    //存当前阅读时的chapterid
    putchapterid(state,data){
      state.chapterid=data
    },

    //存入章节id数组
    putchapteridarr(state,data){
      state.chapteridarr=data
    },

    putsearchlist(state,data){
      state.searchlist=data
    },

    putchapter(state,data){
      state.chapter=data
    },

    //上一章
    async pre(state){
     
      state.chapterid=state.chapterid-1
      // console.log(state.chapterid)
      // console.log(state.chapteridarr.length)
      let data=await axios.get(`http://localhost:3000/chapter/chapter?chapterid=${state.chapteridarr[state.chapterid]}`)
      state.chapterdata=data.data.data[0]
      // console.log(state.chapterdata)
      
     
    },
    //下一章
    async next(state){
      
      state.chapterid=state.chapterid+1
      // console.log(state.chapterid)
      // console.log(state.chapteridarr.length)
      let data=await axios.get(`http://localhost:3000/chapter/chapter?chapterid=${state.chapteridarr[state.chapterid]}`)
      state.chapterdata=data.data.data[0]
      // console.log(state.chapterdata)

      
     
      
    },
    

  },
  actions: {
    //上一章
     Previouschapter(context){
        
        context.commit("pre") 
    },

    //下一章
     nextchapter(context){
     
      context.commit("next") 
  }
  },
  modules: {
  }
})
